package util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import model.QuestionModel;
import model.TagModel;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
import entity.Question;
import entity.Tag;

public class CacheHandle {

	static MemcachedClient memCache = null;

	private static void getInstance() {
		try {
			if (memCache == null) {
				memCache = new MemcachedClient(
						AddrUtil.getAddresses("localhost:11211"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public static void setQuestionCache(String alias) {
		List<String> listAlias = null;
		getInstance();
		Object obj = null;
		try {
			obj = memCache.get("questionCache");			
		} catch (Exception e) {
			// TODO: handle exception
		}

		if (obj != null) {
			listAlias = (List<String>) obj;
			for (int i = 0; i < listAlias.size(); i++) {
				if (listAlias.get(i).equals(alias)) {
					listAlias.remove(i);
				}
			}
			if (listAlias.size() >= 15) {
				listAlias.remove(0);
			}
			listAlias.add(alias);
		} else {
			QuestionModel questioModel = new QuestionModel();
			listAlias = new ArrayList<String>();
			for (Question q : questioModel.all("en", 0)) {
				listAlias.add(q.getAlias());
			}
			listAlias.remove(0);
			listAlias.add(alias);
		}
		memCache.set("questionCache", 600, listAlias);
	}

	public static List<String> getQuestionCache(String lang) {
		QuestionModel questioModel = new QuestionModel();
		getInstance();
		@SuppressWarnings("unchecked")
		List<String> list = (List<String>) memCache.get("questionCache");
		if (list == null || list.size() <= 0) {
			list = new ArrayList<String>();
			for (Question q : questioModel.all("en", 0)) {
				list.add(q.getAlias());
			}			
			memCache.set("questionCache", 600, list);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public static void setTagCache(String tagAlias) {
		List<String> list = null;
		getInstance();
		Object obj = null;
		try {
			obj = memCache.get("tagCache");			
		} catch (Exception e) {
			// TODO: handle exception
		}

		if (obj != null) {
			list = (List<String>) obj;			
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).equals(tagAlias)) {					
					list.remove(i);
				}
			}
			if (list.size() >= 20) {
				list.remove(0);
			}
			list.add(tagAlias);
		} else {
			TagModel tagModel = new TagModel();
			list = new ArrayList<String>();
			for (Tag tag : tagModel.getByCount(19)) {
				list.add(tag.getAlias());
			}			
			list.add(tagAlias);
		}
		memCache.set("tagCache", 600, list);
	}

	public static List<String> getTagCache() {
		TagModel tagModel = new TagModel();
		getInstance();
		@SuppressWarnings("unchecked")
		List<String> list = (List<String>) memCache.get("tagCache");
		if (list == null || list.size() <= 0) {
			list = new ArrayList<String>();
			for (Tag tag : tagModel.getByCount(20)) {
				list.add(tag.getAlias());
			}			
			memCache.set("tagCache", 600, list);
		}
		return list;
	}
}
